package osmedile.intellij.javadochelper;

import com.intellij.openapi.util.text.StringUtil;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class AddClassTagDialog extends JDialog {
    private JPanel contentPane;
    private JButton buttonOK;
    private JButton buttonCancel;
    private JTextField tagNameTxt;
    private JTextField tagValueTxt;
    private JCheckBox ignoreCaseChk;
    private JComboBox tagSameNameCbx;


    private ClassTag classTag;

    private JHConfig jhConfig;

// --------------------------- CONSTRUCTORS ---------------------------

    public AddClassTagDialog() {
        setContentPane(contentPane);
        setModal(true);
        getRootPane().setDefaultButton(buttonOK);


        final DefaultComboBoxModel defaultComboBoxModel1 =
                new DefaultComboBoxModel();
        defaultComboBoxModel1.addElement(ClassTag.REPLACE_MODE.ADD);
        defaultComboBoxModel1.addElement(ClassTag.REPLACE_MODE.DONT_ADD);
        defaultComboBoxModel1.addElement(ClassTag.REPLACE_MODE.REPLACE_ALL);
        tagSameNameCbx.setModel(defaultComboBoxModel1);

        buttonOK.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                onOK();
            }
        });

        buttonCancel.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                onCancel();
            }
        });

// call onCancel() when cross is clicked
        setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                onCancel();
            }
        });

        classTag = new ClassTag();

// call onCancel() on ESCAPE
        contentPane.registerKeyboardAction(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                onCancel();
            }
        }, KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0),
                JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
    }

    private void onOK() {
        //add current values as a new ClassTag

        if (!StringUtil.isEmptyOrSpaces(tagNameTxt.getText())
                && !StringUtil.isEmptyOrSpaces(tagValueTxt.getText())) {
            linkWithClassTag();
            jhConfig.getClassTagModel().add(getClassTag());
        } else {
            //TODO avoid validation, display error messages
            System.out.println("error, tagName and/or tagValue is empty");
        }

        dispose();
    }

    protected void linkWithClassTag() {
        classTag.setName(tagNameTxt.getText());
        classTag.setValue(tagValueTxt.getText());
        classTag.setReplaceMode(
                (ClassTag.REPLACE_MODE) tagSameNameCbx.getSelectedItem());
        classTag.setIgnoreCase(ignoreCaseChk.isSelected());
    }

    private void onCancel() {
        //do nothing
        dispose();
    }

    // --------------------- GETTER / SETTER METHODS ---------------------

    public ClassTag getClassTag() {
        return classTag;
    }

    public JHConfig getJhConfig() {
        return jhConfig;
    }

    public void setJhConfig(JHConfig jhConfig) {
        this.jhConfig = jhConfig;
    }

// -------------------------- OTHER METHODS --------------------------

    {
// GUI initializer generated by IntelliJ IDEA GUI Designer
// >>> IMPORTANT!! <<<
// DO NOT EDIT OR ADD ANY CODE HERE!
        $$$setupUI$$$();
    }

    /**
     * Method generated by IntelliJ IDEA GUI Designer
     * >>> IMPORTANT!! <<<
     * DO NOT edit this method OR call it in your code!
     *
     * @noinspection ALL
     */
    private void $$$setupUI$$$() {
        contentPane = new JPanel();
        contentPane.setLayout(new GridBagLayout());
        final JPanel panel1 = new JPanel();
        panel1.setLayout(new GridBagLayout());
        GridBagConstraints gbc;
        gbc = new GridBagConstraints();
        gbc.gridx = 0;
        gbc.gridy = 1;
        gbc.weightx = 1.0;
        gbc.weighty = 0.1;
        gbc.fill = GridBagConstraints.BOTH;
        contentPane.add(panel1, gbc);
        buttonOK = new JButton();
        buttonOK.setText("OK");
        gbc = new GridBagConstraints();
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.weightx = 1.0;
        gbc.anchor = GridBagConstraints.EAST;
        panel1.add(buttonOK, gbc);
        buttonCancel = new JButton();
        buttonCancel.setHorizontalAlignment(0);
        buttonCancel.setText("Cancel");
        gbc = new GridBagConstraints();
        gbc.gridx = 2;
        gbc.gridy = 0;
        gbc.anchor = GridBagConstraints.EAST;
        panel1.add(buttonCancel, gbc);
        final JPanel panel2 = new JPanel();
        panel2.setLayout(new GridBagLayout());
        gbc = new GridBagConstraints();
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.weightx = 1.0;
        gbc.weighty = 1.0;
        gbc.fill = GridBagConstraints.BOTH;
        gbc.insets = new Insets(0, 10, 0, 10);
        contentPane.add(panel2, gbc);
        final JLabel label1 = new JLabel();
        label1.setText("Tag name");
        gbc = new GridBagConstraints();
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.weightx = 0.2;
        gbc.weighty = 1.0;
        gbc.anchor = GridBagConstraints.WEST;
        panel2.add(label1, gbc);
        tagNameTxt = new JTextField();
        gbc = new GridBagConstraints();
        gbc.gridx = 1;
        gbc.gridy = 0;
        gbc.weightx = 1.0;
        gbc.weighty = 1.0;
        gbc.anchor = GridBagConstraints.WEST;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        panel2.add(tagNameTxt, gbc);
        final JLabel label2 = new JLabel();
        label2.setText("Tag value");
        gbc = new GridBagConstraints();
        gbc.gridx = 0;
        gbc.gridy = 1;
        gbc.weightx = 0.2;
        gbc.weighty = 1.0;
        gbc.anchor = GridBagConstraints.WEST;
        panel2.add(label2, gbc);
        tagValueTxt = new JTextField();
        gbc = new GridBagConstraints();
        gbc.gridx = 1;
        gbc.gridy = 1;
        gbc.weightx = 1.0;
        gbc.weighty = 1.0;
        gbc.anchor = GridBagConstraints.WEST;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        panel2.add(tagValueTxt, gbc);
        ignoreCaseChk = new JCheckBox();
        ignoreCaseChk.setText("Ignore case for tag value");
        gbc = new GridBagConstraints();
        gbc.gridx = 0;
        gbc.gridy = 3;
        gbc.gridwidth = 2;
        gbc.weighty = 0.2;
        gbc.anchor = GridBagConstraints.WEST;
        panel2.add(ignoreCaseChk, gbc);
        final JLabel label3 = new JLabel();
        label3.setText("If other tags with same name");
        gbc = new GridBagConstraints();
        gbc.gridx = 0;
        gbc.gridy = 2;
        gbc.anchor = GridBagConstraints.WEST;
        panel2.add(label3, gbc);
        tagSameNameCbx = new JComboBox();
        final DefaultComboBoxModel defaultComboBoxModel1 =
                new DefaultComboBoxModel();
        tagSameNameCbx.setModel(defaultComboBoxModel1);
        gbc = new GridBagConstraints();
        gbc.gridx = 1;
        gbc.gridy = 2;
        gbc.anchor = GridBagConstraints.WEST;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        panel2.add(tagSameNameCbx, gbc);
    }

    /**
     * @noinspection ALL
     */
    public JComponent $$$getRootComponent$$$() {
        return contentPane;
    }
}
